﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_PERMISSION.Base;
using BaseDAO = ERP_RORZE_TOOL.DBAccess.BaseDAO;

namespace ERP_RORZE_PRODUCT.Product.UserControls
{
    public partial class ctrlBusOderDeadline : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler Reload;
        ClsBusOderDAO clsBusOderDAO = new ClsBusOderDAO();
        DataTable dt;
        ClsBusOderTable b = new ClsBusOderTable();
        ClsMOTable MO = new ClsMOTable();
        ClsMOOptionTable MOO = new ClsMOOptionTable();
        private LogUtils log;

        public ctrlBusOderDeadline()
        {
            InitializeComponent();
            log = new LogUtils();
            this.cbBusOderNo.LostFocus += cbBusOderNo_LostFocus;
        }

        void cbBusOderNo_LostFocus(object sender, EventArgs e)
        {
            try
            {
                if (cbBusOderNo.Text != "")
                {

                    dt = BaseDAO.ExecuteQuery("Select * from [242_BusOder] where [BOderNo]= '" + cbBusOderNo.Text.Trim() + "'");

                    txtPlanNo.Text = dt.Rows[0]["planNo"].ToString();
                    txtMONo.Text = dt.Rows[0]["MONo"].ToString();
                    txtMOQty.Text = dt.Rows[0]["MOQty"].ToString();
                    txtOderQty.Text = dt.Rows[0]["Qty"].ToString();
                    dateDeadline.Value = (DateTime)dt.Rows[0]["Deadline"];
                    checkFinish.Checked = (bool)dt.Rows[0]["Finished"];

                    labelControl3.Visible = true;
                    labelControl4.Visible = true;
                    labelControl9.Visible = true;
                    labelControl7.Visible = true;
                    labelControl6.Visible = true;
                    labelControl10.Visible = true;

                    txtMONo.Visible = true;
                    txtMOQty.Visible = true;
                    txtOderQty.Visible = true;
                    txtPlanNo.Visible = true;
                    checkFinish.Visible = true;
                    dateDeadline.Visible = true;
                }

            }
            catch (Exception)
            {

                MessageBox.Show("No data. Please check you MO Number.");
            }
        }

        private void ctrlBusOderDeadline_Load(object sender, EventArgs e)
        {
            //dt = clsBusOderDAO.GetAllCMB();
            //List<ClsBusOderTable> objs = new List<ClsBusOderTable>();
            //foreach (DataRow r in dt.Rows)
            //{
            //    ClsBusOderTable obj = new ClsBusOderTable();
            //    obj.BOderNo = r["BOderNo"].ToString();  //
            //    obj.PlanNo = r["planNo"].ToString();   ////
            //    obj.Dealine = (DateTime)r["Deadline"];
            //    obj.MONo = r["MONo1"].ToString();     ////
            //    obj.Finished = (bool)r["Finished"];  ////
            //    obj.MOQty = (int)r["MOQty1"];////
            //    obj.Qty = (int)r["Qty"]; /////
            //    objs.Add(obj);
            //}
            //cbBusOderNo.DataSource = objs;
            //cbBusOderNo.DisplayMember = "BOderNo";
        }

        private void cbBusOderNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ClsBusOderTable obj = ((ClsBusOderTable)cbBusOderNo.SelectedItem);
            //txtPlanNo.Text = obj.PlanNo.ToString();
            //txtMONo.Text = obj.MONo.ToString();
            //txtMOQty.Text = obj.MOQty.ToString();
            //txtOderQty.Text = obj.Qty.ToString();
            //dateDeadline.Value = obj.Dealine;
            //checkFinish.Checked = obj.Finished;

            //labelControl3.Visible = true;
            //labelControl4.Visible = true;
            //labelControl9.Visible = true;
            //labelControl7.Visible = true;
            //labelControl6.Visible = true;
            //labelControl10.Visible = true;

            //txtMONo.Visible = true;
            //txtMOQty.Visible = true;
            //txtOderQty.Visible = true;
            //txtPlanNo.Visible = true;
            //checkFinish.Visible = true;
            //dateDeadline.Visible = true;
        }

        private void labelControl3_Click(object sender, EventArgs e)
        {

        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            if (checkFinish.Checked == true)
            {
                MessageBox.Show("Đơn hàng đã hoàn thành. Không thể thay đổi!");
            }
            else
            {
                //MessageBox.Show(""+dateNewDeadline.DateTime);
                ClsBusOderTable obj = new ClsBusOderTable();

                //biến truyền vào để Update vào BusOder
                obj.BOderNo = cbBusOderNo.Text;
                obj.Change = "Deadline Change";
                obj.Dealine = dateNewDeadline.DateTime;
                obj.ChangeDate = dateUpdatedate.Value;
                clsBusOderDAO.DeadlineBusOder(obj);

                //write Log
                log.Table = LogConstant.Table.Product.BusOrderList;
                log.Task = LogConstant.Task.Update;
                log.Description = "Change Deadline BOrderNo: " + obj.BOderNo;
                log.WriteLog();

                //biến truyền vào để update MO
                MO.MONo = txtMONo.Text;
                MO.DeadLine = dateNewDeadline.DateTime;
                MO.Note = "Deadline Changed";
                clsBusOderDAO.DeadlineMO(MO);

                //write Log
                log.Table = LogConstant.Table.Product.MOList;
                log.Task = LogConstant.Task.Update;
                log.Description = "Change Deadline MONo: " + MO.MONo;
                log.WriteLog();

                //truyền biến vào để Update MOOption
                MOO.MONo = txtMONo.Text;
                MOO.Deadline = dateNewDeadline.DateTime;
                clsBusOderDAO.DealineMOOption(MOO);

                //write Log
                log.Table = LogConstant.Table.Product.MOOption;
                log.Task = LogConstant.Task.Update;
                log.Description = "Change Deadline MONo: " + MOO.MONo;
                log.WriteLog();

                ctrlBusOderDeadline_Load(sender, e);
                ////MessageBox.Show("Đã cập nhật thành công");
                //////Reload(sender,e);

                //reset form
                cbBusOderNo.Text = "";
                dateNewDeadline.EditValue = "";
                txtMONo.Text = "";
                txtMOQty.Text = "";
                txtOderQty.Text = "";
                txtPlanNo.Text = "";
                checkFinish.Checked = false;
                txtMOQty.Text = "";
                dateNewDeadline.Text = "";
                cbBusOderNo.Focus();

            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ctrlBusOderDeadline_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.SelectNextControl(this.ActiveControl, true, true, true, true);
            }
        }

        //protected override bool ProcessKeyPreview(ref Message m)
        //{
        //    if (m.Msg == 0x0100 && (int)m.WParam == 13)
        //    {
        //        this.ProcessTabKey(true);
        //    }
        //    return base.ProcessKeyPreview(ref m);
        //}


    }
}
